<?php

/**
 * outil actions.
 *
 * @package    espace14
 * @subpackage outil
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class outilActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->outilpedagogiques = Doctrine_Core::getTable('outilpedagogique')
      ->createQuery('a')
      ->execute();
  }
	public function executeCentrepilote(sfWebRequest $request)
  {
	 $q = Doctrine_Query::create()->from('outilpedagogique o')->where('o.rubrique LIKE ?','0');
     $this->description = $q->fetchOne();
	 $this->centrepilotes = $q->execute(); 
  }
	
	public function executeUnitechirurgicale(sfWebRequest $request)
	{
		$q = Doctrine_Query::create()->from('outilpedagogique o')->where('o.rubrique LIKE ?','2')->execute();
	}
	public function executeUniteformation(sfWebRequest $request)
	{
		$q = Doctrine_Query::create()->from('outilpedagogique o')->where('o.rubrique LIKE ?','3')->execute();
	}
  
  public function executeEquipement(sfWebRequest $request)
  {
	$q = Doctrine_Query::create()->from('outilpedagogique o')->where('o.rubrique LIKE ?','1');
	$this->equipements = $q->execute();
  }
 
  public function executeShow(sfWebRequest $request)
  {
    $this->outilpedagogique = Doctrine_Core::getTable('outilpedagogique')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->outilpedagogique);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new outilpedagogiqueForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new outilpedagogiqueForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($outilpedagogique = Doctrine_Core::getTable('outilpedagogique')->find(array($request->getParameter('id'))), sprintf('Object outilpedagogique does not exist (%s).', $request->getParameter('id')));
    $this->form = new outilpedagogiqueForm($outilpedagogique);
//	$page = $this->form->save();
//	$page->setValue('photourl',$outilpedagogique->getValue('photourl'));
//	$this->form = $page->save();
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($outilpedagogique = Doctrine_Core::getTable('outilpedagogique')->find(array($request->getParameter('id'))), sprintf('Object outilpedagogique does not exist (%s).', $request->getParameter('id')));
    $this->form = new outilpedagogiqueForm($outilpedagogique);

//	$page = $this->form->save();
//	$page->setPhotourl($outilpedagogique->getValue('photourl'));
//	$this->form = $page->save();	
		
    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($outilpedagogique = Doctrine_Core::getTable('outilpedagogique')->find(array($request->getParameter('id'))), sprintf('Object outilpedagogique does not exist (%s).', $request->getParameter('id')));
    $outilpedagogique->delete();

    $this->redirect('outil/index');
  }
  

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
		
		$file = $this->form->getValue('photourl');
		$filename = $file->getOriginalName();
		$extension = $file->getExtension($file->getOriginalExtension());
		$file->save(sfConfig::get('sf_root_dir').sfConfig::get('sf_web_dir_name').sfConfig::get('sf_image_dir_name')
	  .sfConfig::get('sf_upload_dir_name').'/'.$filename);
		//$file->save(sfConfig::get('sf_upload_dir'));
		$img = new sfImage(sfConfig::get('sf_root_dir').sfConfig::get('sf_web_dir_name').sfConfig::get('sf_image_dir_name')
	  .sfConfig::get('sf_upload_dir_name').'/'.$filename,'image/png');
	    //$img = new sfImage(sfConfig::get('sf_upload_dir'));
		$img->resize(300,200);
		$img->save();
		$img->saveAs('thumb'.$filename);
		$page = $form->save();
		$page->setPhotourl($filename);
		$page->setDescription($request->getParameter('editor1'));
		$form = $page->save();
	 $this->redirect('outil/centrepilote');
  //  if ($form->isValid())
  //  {
  //     $outilpedagogique = $form->save();

  //    $this->redirect('outil/edit?id='.$outilpedagogique->getId());
  //  }
  }
}
